package com.jin.spirng_boot_learn.controllers;

import com.jin.spirng_boot_learn.dto.ClassDTO;
import com.jin.spirng_boot_learn.dto.StudentDTO;
import com.jin.spirng_boot_learn.Request.ClassRequest;
import com.jin.spirng_boot_learn.Request.StudentRequest;
import com.jin.spirng_boot_learn.constant.Response;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
public class StudentController {

    @GetMapping("/class/info")
    public @ResponseBody Response getClassInfo(HttpServletRequest request) {

        ServletWebRequest servletWebRequest = new ServletWebRequest(request);
        servletWebRequest.setAttribute("asdf","asdf",RequestAttributes.SCOPE_SESSION);

        StudentDTO xiaoming = new StudentDTO();
        xiaoming.setAge(18);
        xiaoming.setName("xiaoming");

        StudentDTO xiaohong = new StudentDTO();
        xiaohong.setAge(16);
        xiaohong.setName("xiaohong");

        StudentDTO tom = new StudentDTO();
        tom.setAge(17);
        tom.setName("tom");

        List<StudentDTO> studentDTOList = new ArrayList<>();

        studentDTOList.add(xiaohong);
        studentDTOList.add(xiaoming);
        studentDTOList.add(tom);

        ClassDTO classDTO = new ClassDTO();
        classDTO.setGlade("高三");
        classDTO.setNumber(3);
        classDTO.setStudents(studentDTOList);
        return new Response(classDTO);
    }


    @GetMapping("/class/info/session")
    public @ResponseBody Response createClassInfo(HttpServletRequest request) {
        ServletWebRequest servletWebRequest = new ServletWebRequest(request);

        String s = (String)request.getSession().getAttribute("asdf");
        System.out.println(servletWebRequest.getAttribute("asdf",RequestAttributes.SCOPE_SESSION));
        return Response.ok;
    }


    @PostMapping("/class/info/create")
    public @ResponseBody Response createClassInfo(@Validated @RequestBody ClassRequest classRequest) {
        System.out.println(classRequest.getNumber());
        return Response.ok;
    }


    @PostMapping("/student/create")
    public @ResponseBody Response createStudents(@Validated @RequestBody StudentRequest studentRequest) {
        System.out.println(studentRequest.getIdentity());
        return Response.ok;
    }


}
